package com.softserveinc.ita.kaiji.web.rest;

import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.softserveinc.ita.kaiji.model.Card;
import com.softserveinc.ita.kaiji.model.game.Game;
import com.softserveinc.ita.kaiji.model.game.GameHistory;
import com.softserveinc.ita.kaiji.model.game.GameInfo;
import com.softserveinc.ita.kaiji.model.game.RoundResult;
import com.softserveinc.ita.kaiji.model.player.Player;
import com.softserveinc.ita.kaiji.service.GameService;
import com.softserveinc.ita.kaiji.service.GameServiceImpl;

/**
 * @author Eugene Semenkov,Boiko Eduard
 * @version 1.1.2
 * @since 17.03.14
 */

@RestController
@RequestMapping("/rest/game")
public class RestPlayGameController {

	private static Logger logger = Logger.getLogger(RestPlayGameController.class);
	private GameService gameService = new GameServiceImpl();

	@RequestMapping(value = "/{gameId}/", method = RequestMethod.GET )
	public String initGame(@PathVariable Integer gameId, HttpSession session) {
		
		Integer personId = (Integer)session.getAttribute("playerId");

		logger.info("Initialized game. GameId: " + gameId + ". PersonId: " + personId);

		GameHistory gameHistory = gameService.getGameHistory(gameId);
		GameInfo gameInfo = gameHistory.getGameInfo();
		Set<Player> players = gameInfo.getPlayers();
		
		Player person = null;
		Player enemy = null;

		for (Player player : players) {
			if (player.getId().equals(personId)) {
				person = player;
			} else {
				enemy = player;
			}
		}

		session.setAttribute("gameId", gameId);
		session.setAttribute("gameHistory", gameHistory);
		session.setAttribute("playerObject", person);
		session.setAttribute("enemyObject", enemy);

		return "Created game. Game id is " + gameId + ". You can proceed to kaiji/rest/game/" + gameId + "/card/" + "ROCK";

	}

	@RequestMapping(value = "/{gameId}/card/{cardName}/", method = RequestMethod.GET)
	public String getCard(	@PathVariable Integer gameId,
							@PathVariable String cardName,
							HttpSession session) {

		Integer  personId 		= (Integer)session.getAttribute("playerId");
		GameHistory gameHistory = (GameHistory)session.getAttribute("gameHistory");
		Player person 			= (Player)session.getAttribute("playerObject");
		Player enemy 			= (Player)session.getAttribute("enemyObject");
		
		if (logger.isTraceEnabled())
			logger.trace("Starting getCard.");
		
		logger.info("GameId: " + gameId + ". CardId: "
				+ cardName + ". PersonId: " + personId);

		Card playerChosenCard = null;
		for (Card card : Card.values()) {
			if (card.name().equals(cardName)) {
				playerChosenCard = card;
			}
		}

		gameService.makeTurn(gameId, personId, playerChosenCard);

		String playerLastCard = null;
		String botLastCard = null;
		int result = 0;
		for (RoundResult rr : gameHistory.getRoundResults()){
			Card playerCard = rr.getCard(person);
			Card botCard = rr.getCard(enemy);
			result = playerCard.compareTo(botCard);
			playerLastCard = playerCard.toString();
			botLastCard = botCard.toString();
		}
		String playerResult;
		if (result < 0) {playerResult = "LOST";}
		else if (result == 0) {playerResult = "have DRAW";}
		else {playerResult = "WON";}
		int RockLeft = person.getDeck().getCardTypeCount(Card.ROCK);
		int PaperLeft = person.getDeck().getCardTypeCount(Card.PAPER);
		int ScissorsLeft = person.getDeck().getCardTypeCount(Card.SCISSORS);
		String gameStatus = "Game continues";
		if (gameHistory.getGameStatus() == Game.State.GAME_FINISHED){
			gameStatus = "Game finished";
		}

		return
				"Gameid: " + gameId + ".\n" +
				"You played " + playerLastCard + " against " + botLastCard + " and " + playerResult + ".\n" +
				"You have " + RockLeft + " ROCK, " + PaperLeft + " PAPER, " + "and " + ScissorsLeft + " SCISSORS " + ".\n" +
				gameStatus
		;
	}

}
